package com.avic.gateway.common.config;

import com.avic.gateway.common.config.properties.SwaggerProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: huangsheng
 * @date: 2018/8/24
 */
@Component
@Primary
public class SwaggerResources implements SwaggerResourcesProvider {

    @Autowired
    private SwaggerProperties swaggerProperties;

    @Override
    public List<SwaggerResource> get() {
        List resources = new ArrayList<>();
        Map<String, String> swaggerResource = swaggerProperties.getMapProps();
        swaggerResource.forEach((k, v) ->
                resources.add(swaggerResource(k, v, "2.0")));
        return resources;
    }

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }
}
